﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;


using DotSpatial.Topology;

namespace HaGisPack
{

    public class SelfPoint
    {
        public double X;
        public double Y;
        public double Z;
    

    }


    
    public class MyArithmetic
    {

        /// <summary>
        /// 根据圆心、半径求圆上其它点
        /// </summary>
        /// <param name="CenterPoint">圆心</param>
        /// <param name="r">半径</param>
        /// <param name="num">圆上点的个数</param>
        /// <returns></returns>
        public static List<Coordinate> GetCommonPoints(Coordinate CenterPoint, double r, int num)
        {
            if (CenterPoint == null) return null;

            if (num == 0) return null;

            List<Coordinate> CirclePoints = new List<Coordinate>();

            for (int i = 0; i < num; i++)
            {
                Coordinate pt = new Coordinate();
                
                double Angle;
                Angle = 2 * Math.PI * i / num;

                pt.X = CenterPoint.X + r * Math.Cos(Angle);
                pt.Y = CenterPoint.Y + r * Math.Sin(Angle);

                CirclePoints.Add(pt);
                                         
            }

            return CirclePoints;
        
        }

        /// <summary>
        /// 根据圆心、半径求画圆时的关键点
        /// </summary>
        /// <param name="CenterPoint">圆心</param>
        /// <param name="r">半径</param>
        /// <returns></returns>
        public static Coordinate GetKeyPoint(Coordinate CenterPoint, double r)
        {
            Coordinate KeyPoint = new Coordinate();

            KeyPoint.X = CenterPoint.X + r;
            KeyPoint.Y = CenterPoint.Y;

            return KeyPoint;
        }

        //========================================================================================================

        //以下是矩形算法

        /// <summary>
        /// 求矩形的各个参数
        /// </summary>
        /// <param name="pts">矩形四个点</param>
        /// <param name="XLength">所求矩形的X边长</param>
        /// <param name="YLength">所求矩形的Y边长</param>
        /// <param name="Angle">所求矩形的旋转角度</param>
        public static void GetRectPara(Coordinate[] Points, ref double XLength, ref double YLength, ref double Angle)
        {
            //第二个点在第一个点和第三个点的哪一侧
            double direct;
            direct = (Points[0].X - Points[1].X) * (Points[2].Y - Points[1].Y) - (Points[0].Y - Points[1].Y) * (Points[2].X - Points[0].Y);

            if (direct > 0) //左侧
            {
                XLength = Math.Sqrt((Points[0].X - Points[1].X) * (Points[0].X - Points[1].X) + (Points[0].Y - Points[1].Y) * (Points[0].Y - Points[1].Y));
                YLength = Math.Sqrt((Points[0].X - Points[3].X) * (Points[0].X - Points[3].X) + (Points[0].Y - Points[3].Y) * (Points[0].Y - Points[3].Y));
            }
            else  //右侧
            {
                XLength = Math.Sqrt((Points[0].X - Points[3].X) * (Points[0].X - Points[3].X) + (Points[0].Y - Points[3].Y) * (Points[0].Y - Points[3].Y));
                YLength = Math.Sqrt((Points[0].X - Points[1].X) * (Points[0].X - Points[1].X) + (Points[0].Y - Points[1].Y) * (Points[0].Y - Points[1].Y));
            }


            Angle = Math.Atan((Points[2].Y - Points[0].Y) / (Points[2].X - Points[0].X));

            if (Points[2].Y > Points[0].Y && Points[2].X < Points[0].X)
            {
                Angle = 180 + Angle;
            }

            if (Points[2].Y > Points[0].Y && Points[2].X == Points[0].X)
            {
                Angle = 90;
            }

            if (Points[2].Y < Points[0].Y && Points[2].X < Points[0].X)
            {
                Angle = Angle - 180;
            }
            if (Points[2].Y < Points[0].Y && Points[2].X == Points[0].X)
            {
                Angle = -90;
            }
            if (Points[2].Y == Points[0].Y && Points[2].X > Points[0].X)
            {
                Angle = 0;
            }
            if (Points[2].Y == Points[0].Y && Points[2].X < Points[0].X)
            {
                Angle = 180;
            }
        }


        public static Coordinate[] GetRectPoint(Coordinate FirstPoint, double XLength, double YLength, double Angle)
        {
            Coordinate[] RectPoints = new Coordinate[3];

            //对角线长
            double length = Math.Sqrt(XLength * XLength + YLength * YLength);

            //第一个点
            RectPoints[0].X = FirstPoint.X;
            RectPoints[0].Y = FirstPoint.Y;

            //第三个点
            RectPoints[2].X = FirstPoint.X + length * Math.Cos(Angle / 180 * Math.PI);
            RectPoints[2].Y = FirstPoint.Y + length * Math.Sin(Angle / 180 * Math.PI);

            //第二个点
            double Angle1 = Math.Atan(YLength / XLength) + Angle / 180 * Math.PI; //第二个点与第一个点的夹角
            RectPoints[1].X = FirstPoint.X + XLength * Math.Cos(Angle1);
            RectPoints[1].X = FirstPoint.X + XLength * Math.Sin(Angle1);

           
            //第四个点
            RectPoints[3].X = RectPoints[0].X + RectPoints[2].X - RectPoints[1].X;
            RectPoints[3].Y = RectPoints[0].Y + RectPoints[2].Y - RectPoints[1].Y;
            
            return RectPoints;
        }




        //============================================================================================================//

        //颜色算法

        /// <summary>
        /// 根据已知的颜色集合，创建新的更详细的颜色集合
        /// </summary>
        /// <param name="ColorList">已知的颜色集合</param>
        /// <returns></returns>
        public static List<System.Drawing.Color> BuildColor(List<System.Drawing.Color> ColorList)
        {
            int N = ColorList.Count;   //给定的渐变色个数

            if (N < 1) return null;

            List<Color> tempColorList = new List<Color>(); //存储计算出的更加详细的渐变色

            if (N > 1)
            {
                Color a, b;

                for (int i = 0; i < N - 1; i++)
                {
                    //两个相邻的渐变色
                    a = ColorList[i];
                    b = ColorList[i + 1];

                    //取其三种组成色，用于计算
                    double aRed = a.R;
                    double aGreen = a.G;
                    double aBlue = a.B;
                    double bRed = b.R;
                    double bGreen = b.G;
                    double bBlue = b.B;

                    //把两个相邻的渐变色再平均分成10000份，使颜色列表更加详细……
                    for (int j = 1; j <= 10000; j++)
                    {
                        double red = aRed + (bRed - aRed) / 10000 * j;
                        double green = aGreen + (bGreen - aGreen) / 10000 * j;
                        double blue = aBlue + (bBlue - aBlue) / 10000 * j;

                        Color tempColor = Color.FromArgb((int)red, (int)green, (int)blue);
                        tempColorList.Add(tempColor);
                    }
                }

            }
            else //只有一种颜色的情况
            {
                tempColorList.Add(ColorList[0]);
            }

            return tempColorList;
        }



        /// <summary>
        /// 得到第Pos个等值线的颜色在颜色集合中的位置
        /// </summary>
        /// <param name="ColorNum">颜色的数量</param>
        /// <param name="LinePos">等值线在等值线集合中的位置</param>
        /// <param name="LineNum">等值线的数量</param>
        /// <param name="minVal">等值线值最小值</param>
        /// <param name="maxVal">等值线值最大值</param>
        /// <returns></returns>
        public static int GetColorPos(int ColorNum, int LinePos, int LineNum,double minVal,double maxVal)
        {
            int ColorPos; //根据单一等值线在等值线集合中的位置，求得要赋予此等值线的颜色在颜色集合中的位置……


            if (ColorNum == 1)
            {
                ColorPos = 0;
            }
            else
            {
                if (minVal != maxVal)
                {
                    ColorPos = LinePos * ColorNum/ LineNum  - 1;
                }
                else
                {
                    ColorPos = 0;
                }
            }


            if (ColorPos > ColorNum - 1) //这个判断没有意义嘛！！！！！！！！！为什么付还要写？？？？
                ColorPos = ColorNum - 1;

            if (ColorPos < 0)
                ColorPos = 0;

            return ColorPos;

        }









    }
}
